//////////////////////////////////////////////////////////////////////////////////////////////////////////
// $Id: MyArticles.js,v 1.5 2001/11/30 22:50:50 sean Exp $                                  
// MyArticles.js - Library used for MyArticles                                                          //
//                                                                                                      //
// Requires JavaScript1.3                                                                               //
//                                                                                                      //
// Dependencies:  Before importing this library into your code, you must first import cookie.js.        //
//                                                                                                      //
// Author:  Sean Showalter                                                                              //
// Created: October 2001
////////////////////////////////////////////////////////////////////////////////////////////////////////// 


// GLOBALS //

// Our global cookie object.  See cookie.js for usage.
var MyCookie = new Cookie(document, "SciServ-MyArticles", 1, "/cgi-bin"); 

// Note: IE limited to 4k, but Netscape not.  So, you can boost this up to 4000 if needed.
var MAX_COOKIE_SIZE = 3500; 

// GLOBALS //


////
// isCheckboxChecked - Adds/removes article keys from cookie.  Once the user clicks on the box, we
//                     see if it is has already been checked.  If it has, then this indicates that 
//                     the user has changed his/her mind and has decided not to add this to the list
//                     of items they want saved.  However, if it is not checked, then this indicates
//                     that they wish to add this item to the list (or cookie).
//
// @param	the current checkbox
// @return	true or false
//
function isCheckboxChecked(my_checkbox) 
{
    if (my_checkbox.checked) {

        // If the size of the checkbox value + the existing cookie is larger than
	// MAX_COOKIE_SIZE, then return a false value.
        if (!checkCookieSize(my_checkbox.value)) {
	    my_checkbox.checked = false;
	    return false;
	}

	if (MyCookie.load() && MyCookie.articles) {
	    // We have existing entries, so append value to cookie using a ':' delimiter
	    MyCookie.articles += ':' + my_checkbox.value; 

	// This is our first entry, so don't append using a ':' delimiter
	} else {  
	    MyCookie.articles = my_checkbox.value;
	}
        
	MyCookie.store();

    } else { 
        deleteMyArticlesValue(my_checkbox);  
    }

    return true;
}


////
// deleteMyArticlesValue - Deletes value from MyCookie.
//
// @param	Result string, which is the value of the checkbox clicked on.
// @return	true or false
//
function deleteMyArticlesValue(my_checkbox)
{
    if (!MyCookie.load() || !MyCookie.articles) {
	return false;
    }

    var values = MyCookie.articles;
    if (values.indexOf(':') != -1) { // this is a list of values
        var p_values = values.split(':');  
        
	// First we clear the articles from MyCookie (we've already stored it in our array)
        MyCookie.articles = "";  

        // Next, we write out the MyCookie again, but skip the entry we don't want.
        for (var i = 0; i < p_values.length; i++) {
            if (p_values[i] == my_checkbox.value) {
		continue;  // skip unwanted entry
	    }

            if (!MyCookie.articles) { 

	        // This is the first entry, and we don't want 
		// a : appended to the front of the value.

	        MyCookie.articles = p_values[i];

	    } else {

	        MyCookie.articles += ':' + p_values[i];

	    }
        }

    } else { // The cookie only contains one entry so we just undef it
             // rather than do anything fancy for removing the one value. 

        MyCookie.articles = "";
    }

    MyCookie.store();
    return true;
}

////
// clearMyArticlesCookie() - Sets the MyArticles cookie to undef, if it exists.
//
// @param	none
// @return	true 
//
function clearMyArticlesCookie()
{
    if(MyCookie.load() && MyCookie.articles) {
        MyCookie.articles = '';
        MyCookie.store();
    }

    return true;
}

////
// checkCookieSize - Disallows cookie to get larger than MAX_COOKIE_SIZE
//
// @param	result string
// @return	true or error message and false
//
function checkCookieSize(article)
{
    var a = article.toString();
    a = a.length;
    var size = document.cookie.length + a + 1;  // the "+ 1" is to account for the ":" delimiter

    if (size > MAX_COOKIE_SIZE) {
        if (navigator.appName == 'Netscape') {
	    window.open('/alerts/max_results.html','max_results','height=150,width=350,screenX=300,screenY=200,resizable=yes');
	} else {  // assume IE
	    window.open('/alerts/max_results.html','max_results','height=150,width=350,left=300,top=200,resizable=yes');
	}

	return false;
    }

    return true;
}


////
// submitMyArticlesForm - determines command, sets command param, and submits form
//
// @param	command		value that command should be set to (required)
// @param	checkCookie	passed to validateMyArticlesForm (optional)
// @return	false
//
function submitMyArticlesForm(command,checkCookie)
{
    // sets the target window to current window 
    // -- we do this in case the target was set to a different window previoulsy
    document.MyArticles.target = "_self";  

    if (command == 'store') {
        var myFile = '/alerts/MyArticlesSaveErrorMsg.html';
        if (!validateMyArticlesForm(myFile, checkCookie)) {
	    return false;
	}
        document.MyArticles.command.value = 'store';

	var confirmWindow;    // reference to confirmaton window object

	if (navigator.appName == 'Netscape') {
	    confirmWindow = window.open('','confirm','height=200,width=300,screenX=300,screenY=200,resizable=yes');
	} else {  // assume IE
	    confirmWindow = window.open('','confirm','height=200,width=300,left=300,top=200,resizable=yes');
	}

        confirmWindow.document.write('<html><head><title>Confirmation</title></head><body bgcolor="#FFFFFF"><center><font face="arial" size="2"><P>Please wait...</font></center></body></html>');
	confirmWindow.document.close();

	document.MyArticles.target = "confirm";
	document.MyArticles.submit();
	resetMyArticlesForm();
    }
    else if (command == 'get') {
        document.MyArticles.command.value = 'get';
    	document.MyArticles.submit();
    }
    else if (command == 'remove') {
        document.MyArticles.command.value = 'remove';
    	document.MyArticles.submit();
    }
    else if (command == 'removeall') {
        document.MyArticles.command.value = 'removeall';
    	document.MyArticles.submit();
    }
    else if (command == 'search') {
        var myFile = '/alerts/MyArticlesViewErrorMsg.html';
        if (!validateMyArticlesForm(myFile,checkCookie)) {
	    return false;
	}
        document.MyArticles.action='/cgi-bin/search.pl/SaveChecked'
    	document.MyArticles.submit();
    } 
    else {
        alert('command ' + command + ' not valid');
    }

    return false;
}


////
// resetMyArticlesForm() - resets MyArticles form and clears MyArticles cookie
//
// @param	commmand	used to clear cookie (hence 'cc')
// @return	false
//
function resetMyArticlesForm(command)
{
    if (command == 'cc') {
	clearMyArticlesCookie();
    }
    document.MyArticles.reset();
    return false;
}

////
// fillInCheckboxes - Fills in checkboxes for results stored in MyCookie
//
// @param	none
// @return	true or false
//
function fillInCheckboxes()
{
    if (!MyCookie.load() || !MyCookie.articles) {
	return false;
    }

    var articles = MyCookie.articles.split(':');  

    // loop through all form elements, looking for the Article and Issue keys
    for (var x = 0; x < document.MyArticles.length; x++) {

        var e = document.MyArticles.elements[x];  // the current form element

        if (e.name == 'ArticleKey') {  

            // now let's loop through our array and check off any boxes that match
            for (var i = 0; i < articles.length; i++) {

		if (e.value == articles[i]) {
	            document.MyArticles.elements[x].checked = true;
		    break;
		}
	    }
	}
    }

    return true;
}


////
// function validateMyArticlesForm() - Keeps user from submitting an empty form
//
// @param	myFile		file that contains error message (required)
// @param	checkCookie	designates to check for cookie value (optional)
function validateMyArticlesForm(myFile, checkCookie)
{
    if (checkCookie && MyCookie.load() && MyCookie.articles) {
	return true;
    }


    // loop through all form elements, looking for the checkboxes
    for (var x = 0; x < document.MyArticles.length; x++) {

        var e = document.MyArticles.elements[x];  // the current form element

        if (e.name == "ArticleKey" && e.checked) {
	    return true;
	}
    }

    if (navigator.appName == 'Netscape') {
        window.open(myFile,'error','height=200,width=250,screenX=300,screenY=200,resizable=yes');
    } else {  // assume IE
        window.open(myFile,'error','height=200,width=250,left=300,top=200,resizable=yes');
    }

    return false;
}


